package com.zzd.controller;

import com.zzd.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class AjaxController {
    @RequestMapping("/a1.action")
    public void ajax1(String name, HttpServletResponse response) throws IOException {
        if ("admin".equals(name)){
            response.getWriter().print("true");
        }else {
            response.getWriter().print("false");
        }
    }

    @RequestMapping("/a2")
    @ResponseBody
    public List<User> ajax2(){
        List<User> users = new ArrayList<>();
        users.add(new User("wa",18,"女"));
        users.add(new User("wb",23,"男"));
        users.add(new User("wc",25,"男"));
        users.add(new User("wd",16,"女"));
        return users;
    }

    @RequestMapping(value = "/a3",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String ajax3(String name,String pwd){
        String msg = "";
        if(name !=null){
            if("admin".equals(name)){
                msg = "ok";
            }else{
                msg= "用户名错误";
            }
        }
        if(pwd !=null){
            if("12345".equals(pwd)){
                msg="ok";
            }else {
                msg = "密码错误";
            }
        }
        return msg;
    }
}
